package cn.hiapi.admin.query;

import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.jpa.query.BasicQuery;
import cn.hiapi.shopkeeper.enums.ActivityType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author AdinZ
 * @date 2022/12/30 15:16
 */
@Setter
@Getter
public class ActivityQuery extends BasicQuery {
    @ApiModelProperty("分类id")
    private Long cid;
    @ApiModelProperty("活动id")
    private ActivityType type;
    @ApiModelProperty("活动标题")
    private String title;
    @ApiModelProperty("店铺id")
    private List<Long> shopId;
    @ApiModelProperty("状态")
    private Integer status;
    @ApiModelProperty("店铺名字")
    private String shopName;

    @Override
    public QueryWrapper getQueryWrapper() {
        QueryWrapper wrapper = super.getBasicQueryWrapper();

        if (status != null) {
            if (status == 0) {
                //未开始 开始时间< 当前时间
                wrapper.lt("startTime", System.currentTimeMillis());
            } else if (status == 1) {
                //进行中, 开始时间>当前时间 ,  结束时间< 当前时间
                wrapper.gt("startTime", System.currentTimeMillis())
                        .lt("endTime", System.currentTimeMillis());
            } else if (status == 2) {
                //结束, 结束时间< 当前时间
                wrapper.lt("endTime", System.currentTimeMillis());
            }
        }
        wrapper.in("shopId", this.shopId)
                .eq("cid", cid)
                .eq("type", this.type);
        if (StringUtils.isNotEmpty(title))
            wrapper.like("title", StringUtils.join("%", this.title, "%"));
        return wrapper;
    }
}
